package com.business.business.web.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.business.business.common.base.BaseService;
import com.business.business.model.Picture;
import com.business.business.support.model.RespEntity;
import com.business.business.support.mybatisplus.QueryWrapper;
import com.business.business.support.utils.requestutil.HttpRequestUtil;
import com.business.business.web.mapper.PictureMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class PictureService extends BaseService {
    private static final Logger logger = LoggerFactory.getLogger(PictureService.class);

    @Resource
    private PictureMapper pictureMapper;

    //新增
    public RespEntity add(JSONObject jsonParam) {
//        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
//        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
//        //调用token验证接口
//        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
//        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));

        String fileName = jsonParam.getStr("fileName");
        String filePath = jsonParam.getStr("filePath");
        String businessId = jsonParam.getStr("businessId");
        String id = UUID.randomUUID().toString();

        Picture picture = new Picture();
        picture.setId(id);
        picture.setFileName(fileName);
        picture.setFilePath(filePath);
        picture.setBusinessId(businessId);
        int insert = pictureMapper.insert(picture);
        if (insert > 0) {
            return RespEntity.success();
        } else {
            return RespEntity.fail();
        }
    }

    //删除
    public RespEntity delete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));

        String filePath = jsonParam.getStr("filePath");
        String businessId = jsonParam.getStr("businessId");

        if (filePath != null && filePath != "" && businessId != null && businessId != "") {
            int delete = this.pictureMapper.delete(new UpdateWrapper<Picture>().eq("file_path", filePath).eq("business_id", businessId));
            if (delete > 0) {
                return RespEntity.success();
            } else {
                return RespEntity.fail();
            }
        }
        return RespEntity.fail();
    }

    //图片列表
    public RespEntity pictureList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        //调用token验证接口
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String businessId = jsonParam.getStr("businessId");
        List<Picture> pictures = this.pictureMapper.selectList(new QueryWrapper<Picture>().eq("business_id", businessId));
        return RespEntity.success(pictures);
    }
}
